//
//  MyCollectionViewCell.m
//  Funny
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 King. All rights reserved.
//

#import "MyCollectionViewCell.h"

@implementation MyCollectionViewCell

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.tableView];
        [self.contentView addSubview:self.pic];
        self.pic.image = [UIImage imageNamed:@"icon_cancle_publish@2x.png"];
        [self.contentView addSubview:self.lable];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(deleteWithNoti:) name:@"delete" object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeWithNoti:) name:@"change" object:nil];
    }
    return self;
}

- (void)changeWithNoti:(NSNotification *)noti
{
    self.flag = [[noti.userInfo objectForKey:@"if"]integerValue];
}

- (void)deleteWithNoti:(NSNotification *)noti
{
    NSInteger open = [[noti.userInfo objectForKey:@"do"] integerValue];
    switch (open) {
        case 0:
            _tableView.editing = YES;
            break;
        case 1:
            _tableView.editing = NO;
            break;
        default:
            break;
    }
}

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SelfWidth, SelfHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellAccessoryNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"reuse"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_arr.count == 0) {
        self.lable.alpha = 1;
        self.pic.alpha = 1;
    }else{
        self.lable.alpha = 0;
        self.pic.alpha = 0;
    }
    return _arr.count;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete || editingStyle == UITableViewCellEditingStyleInsert) {
        [[DataBase shareDataBase]open];
        switch (self.flag) {
            case 0:
                [[DataBase shareDataBase]deleteInlandDataWithModel:_arr[indexPath.row]];
                
                break;
            case 1:
                [[DataBase shareDataBase]deleteDataWithModel:_arr[indexPath.row]];
                break;
            case 2:
                [[DataBase shareDataBase] deleteDetailDataWithModel:_arr[indexPath.row]];
                break;
            default:
                break;
        }
        
        [self.arr removeObject:[_arr objectAtIndex:indexPath.row]];
        [_tableView reloadData];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if ([_arr[0] isKindOfClass:[MainIntroductionModel class]]) {
        cell.model = _arr[indexPath.row];
    }
    if ([_arr[0] isKindOfClass:[Detail class]]) {
        cell.detailModel = _arr[indexPath.row];
    }
    if ([_arr[0] isKindOfClass:[FoodInfoModel class]]) {
        cell.infoModel = _arr[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FoodInfoModel *model = _arr[indexPath.row];
    [self.delegate pushToNextWithModel:model.cityID];
}

- (UILabel *)lable
{
    if (!_lable) {
        self.lable = [[UILabel alloc]initWithFrame:CGRectMake(SelfWidth / 2 - 50, SelfHeight / 2, 100, 30)];
        self.lable.textAlignment = NSTextAlignmentCenter;
        self.lable.textColor = [UIColor grayColor];
        self.lable.text = @"暂无收藏";
    }
    return _lable;
}

- (UIImageView *)pic
{
    if (!_pic) {
        self.pic = [[UIImageView alloc]initWithFrame:CGRectMake(SelfWidth / 2 - 50, SelfHeight / 2 - 110 * H, 100 * H, 100 * H)];
    }
    return _pic;
}

- (void)setArr:(NSMutableArray *)arr
{
    _arr = arr;
    [self.tableView reloadData];
}


@end
